angular.module('starter.controllers', [])

	.controller('DashCtrl', function($scope, $state) {
		$scope.demonstration = function(){
			$state.go('tab.demonstration')
		}
	})

	/*.controller('FriendsCtrl', function($scope, Friends) {
	 $scope.friends = Friends.all();
	 })

	 .controller('FriendDetailCtrl', function($scope, $stateParams, Friends) {
	 $scope.friend = Friends.get($stateParams.friendId);
	 })*/

	.controller('CaseCtrl', function($scope) {
	})

	//投保
	.controller('InsureCtrl', function($scope, $state, $http, $stateParams) {

		$scope.model1=[
			{period:'终身'}
		];
		$scope.selected1 = $scope.model1[0];

		$scope.model2=[
			{frequency:'1'}
		];
		$scope.selected2 = $scope.model2[0];

		$scope.model3=[
			{years:'3'},
			{years:'5'},
			{years:'10'}
		];
		for(var i=0;i<$scope.model3.length;i++){
			if($scope.model3[i].years == storage.selectedYears){
				$scope.selected3 = $scope.model3[i];
			}
		}


//		console.log($scope.selected3)

		/*if(storage.selectedYears){
			$scope.selected3 = storage.selectedYears+'年';
		}else{
			$scope.selected3 = $scope.model3[0].years+'年';
		}*/


		$scope.model4=[
			{insured_gender:'男'},
			{insured_gender:'女'}
		];
		for(var i=0;i<$scope.model4.length;i++){
			if($scope.model4[i].insured_gender == storage.selectedGender){
				$scope.selected4 = $scope.model4[i];
			}
		}

		$scope.model5=[
			{buy_num:'3'},
			{buy_num:'4'},
			{buy_num:'5'},
			{buy_num:'6'},
			{buy_num:'7'},
			{buy_num:'8'},
			{buy_num:'9'},
			{buy_num:'10'}
		];
		for(var i=0;i<$scope.model5.length;i++){
			if($scope.model5[i].buy_num == storage.selectedBuyNum){
				$scope.selected5 = $scope.model5[i];
			}
		}

//		$scope.selected5 = storage.selectedBuyNum;

		$scope.model6=[
			{start_age:'55'},
			{start_age:'60'},
			{start_age:'65'}
		];
		for(var i=0;i<$scope.model6.length;i++){
			if($scope.model6[i].start_age == storage.selectedStartAge){
				$scope.selected6 = $scope.model6[i];
			}
		}

		$scope.model7=[
			{policy_gender:'男'},
			{policy_gender:'女'}
		];
//		$scope.selected7.policy_gender = '';
		for(var i=0;i<$scope.model4.length;i++){
			if($scope.model7[i].policy_gender == storage.selectedGender){
				$scope.selected7 = $scope.model7[i];
			}
		}


		$scope.insureInfo = {insured_age:storage.selectedAge};

		$scope.insureSubmit = function(insureInfo){
//			console.log($scope.selected4.insured_gender,$scope.selected7);

			$http.get(API_URl + 'action?insured_name='+insureInfo.insured_name+'&insured_sex='+$scope.selected4.insured_gender+'&insured_age='+insureInfo.insured_age+'&insured_cardno='+insureInfo.insured_cardno+'&insured_phone='+insureInfo.insured_phone+'&insured_address='+insureInfo.insured_address+'&policy_name='+insureInfo.policy_name+'&policy_sex='+$scope.selected7.policy_gender+'&policy_age='+insureInfo.policy_age+'&policy_cardno='+insureInfo.policy_cardno+'&policy_phone='+insureInfo.policy_phone+'&policy_address='+insureInfo.policy_address+'&payperiod='+$scope.selected3.years+'&premium='+$scope.selected5.buy_num*1000+'&receive='+$scope.selected6.start_age+'&proid=1').success(function(data){
				storage.insureId = data.data;

				if(data.status.code==0){
					$state.go('tab.insure-submit');
				}
			})
		}

	})


	.controller('InsureSubmitCtrl', function($scope, $state, $stateParams) {
		$scope.insureId=storage.insureId;
//		console.log(storage.insureId)

		$scope.insurePlan=function(){
			$state.go('tab.create-demonstration')
		}
	})
	.controller('DemonstrationCtrl', function($scope, $state) {
		localStorage.removeItem("insureId");
		$scope.selected1 = '';
		$scope.model1=[
			{period:'终身'}
		];
		$scope.selected2 = '';
		$scope.model2=[
			{frequency:'1'}
		];
		$scope.selected3 = '';
		$scope.model3=[
			{years:'3'},
			{years:'5'},
			{years:'10'}
		];
		$scope.selected4 = '';
		$scope.model4=[
			{gender:'男'},
			{gender:'女'}
		];
		$scope.selected5 = '';
		$scope.model5=[
			{buy_num:'3'},
			{buy_num:'4'},
			{buy_num:'5'},
			{buy_num:'6'},
			{buy_num:'7'},
			{buy_num:'8'},
			{buy_num:'9'},
			{buy_num:'10'}
		];
		$scope.selected6 = '';
		$scope.model6=[
			{start_age:'55'},
			{start_age:'60'},
			{start_age:'65'}
		];

		$scope.createDemo = function(demoInfo){
			$state.go('tab.create-demonstration');

			/*if(demoInfo.selected1.period==undefined||demoInfo.selected2.frequency==undefined||demoInfo.selected3.years==undefined||demoInfo.age==''||demoInfo.selected4.gender==undefined||demoInfo.selected5.buy_num==undefined||demoInfo.selected6.start_age==undefined){
				alert('请填写完整！')
			}else{*/
				storage.selectedPeriod = demoInfo.selected1.period;
				storage.selectedFrequency = demoInfo.selected2.frequency;
				storage.selectedYears = demoInfo.selected3.years;
				storage.selectedAge = demoInfo.age;
				storage.selectedGender = demoInfo.selected4.gender;
				storage.selectedBuyNum = demoInfo.selected5.buy_num;
				storage.selectedStartAge = demoInfo.selected6.start_age;
//			}

		};

	})
	.controller('CreateDemonstrationCtrl', function($scope, $state, $http, $sce) {
		console.log(storage.selectedPeriod);
		$scope.insure = function(){
			$state.go('tab.insure')
		};

		var url = API_URl + '001?payperiod='+storage.selectedYears+'&age_1='+storage.selectedAge+'&sex_1='+storage.selectedGender+'&portion='+storage.selectedBuyNum+'&receive='+storage.selectedStartAge+'&proid=1&premium='+storage.selectedBuyNum*1000;
		if(storage.insureId){
			url = API_URl + '002?insuid='+storage.insureId;
		}

//		alert(storage.insureId)
		$http.get(url).success(function(data){
			var arg = data;
			var rows = arg.data[0].table_1.rows;//行
			var cols = arg.data[0].table_1.cols;//列
			var str = "<div class='content-text'><p>";

			if(arg.data[0].table_2.t_name != null){
				str += "尊敬的:"+arg.data[0].table_2.t_name;
				str += ""+ arg.data[0].table_2.t_sex == 0 ? "先生":"女士";
				//str += "投保人年龄:"+arg.data[1].table_2.t_age;
				str += arg.data[0].table_2.b_name;
			}


			str += ""+arg.data[0].table_2.b_sex == 0 ? "先生":"女士</p>";
			//str += "被保人年龄:"+arg.data[1].table_2.b_age;
			str += "<p>选择的华夏南山松养老年金保险：</p>";

			str += "<p>年交保费为："+arg.data[0].table_2.gp +"</p>";
			str += "<p>交费年期为："+arg.data[0].table_2.payperiod +"</p>";
			str += "<p>年金起领年龄为："+arg.data[0].table_2.receive +"</p>";
			str += "<p>保险金额为："+arg.data[0].table_2.sa +"</p>";
			str += "</div>";

			str += "<table class='item-table'>";
			for(var i = 0 ; i < rows ; i ++){
				str = str + "<tr>";
				for(var k = 1 ; k <= cols ; k++){
					str = str + "<td align=center bgcolor=#ffffff>";
					str = str + arg.data[0].table_1[i][k];
					str = str + "</td>";
				}
				str = str + "</tr>";
			}
			str = str + "</table>";
			$scope.dataContent=str;
			$scope.dataContentHtml = function() {
				return $sce.trustAsHtml($scope.dataContent);
			};
		});
	})


//产品详情，产品特色tab
	.controller('TabsCtrl', ['$scope', function ($scope) {
		$scope.tabItems = [{
			title: '产品详情',
			url: 'templates/tab-tablist01.html'
		}, {
			title: '产品特色',
			url: 'templates/tab-tablist02.html'
		}];

		$scope.currentTab = 'templates/tab-tablist01.html';

		$scope.onClickTab = function (tabItem) {
			$scope.currentTab = tabItem.url;
		};

		$scope.isActiveTab = function(tabUrl) {
			return tabUrl == $scope.currentTab;
		}
	}])
